package com.dg11185.nearshop.activity.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.nineoldandroids.animation.ObjectAnimator;
import com.umeng.analytics.MobclickAgent;
import com.wlper.lib.uitls.ViewUtils;

public class UserInfoModifyActivity extends Activity implements OnClickListener {

    /**标记是修改那个 */
    public static final int USER_INFO_REALNAME = 0x0001;
    
    public static final int USER_INFO_SIGN = 0x0002;
    
    public static final int USER_INFO_IDCARD = 0x0003;
    
    private int length; // intent传入的值，用于限制用户输入长度

    /***/
    private TextView back, title, finish, notice;

    private EditText content;

    private ImageView delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info_modify);

        back = (TextView) this.findViewById(R.id.modify_back);
        title = (TextView) this.findViewById(R.id.modify_title);
        finish = (TextView) this.findViewById(R.id.modify_finish);
        notice = (TextView) this.findViewById(R.id.user_info_edit_notice);
        back.setOnClickListener(this);
        finish.setOnClickListener(this);

        content = (EditText) this.findViewById(R.id.user_info_eidt_txt);
        content.addTextChangedListener(watcher);
        delete = (ImageView) this.findViewById(R.id.user_info_eidt_img);
        delete.setOnClickListener(this);
        Intent intent = getIntent();
        content.setText(intent.getStringExtra("content"));
        title.setText(intent.getStringExtra("title"));
        length = intent.getIntExtra("length", 0);
        notice.setText("0/" + length);
    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if ((start + count) > length) {
                notice.setText("内容超出！ " + (start + count) + "/" + length);
                if (start <= length) {
                    notice.setTextColor(getResources().getColor(R.color.main_red));
                }
            } else {
                notice.setText((start + count) + "/" + length);
                if (start > length) {
                    notice.setTextColor(getResources().getColor(R.color.font_dark_gray));
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
        case R.id.modify_back:
            this.setResult(RESULT_CANCELED);
            this.finish();
            break;

        case R.id.modify_finish:
            if (content.getText().length() > length) {
                ViewUtils.showToast(this, "内容超过预定长度");
                break;
            }
            if (content.getText().length() == 0) {
                ViewUtils.showToast(this, "内容不能为空");
                break;
            }
            intent.putExtra("content", content.getText().toString().trim());
            this.setResult(RESULT_OK, intent);
            this.finish();
            break;
        case R.id.user_info_eidt_img:
            ObjectAnimator.ofFloat(delete, "rotation", 0, 720, 360).setDuration(1000).start();
            content.setText("");
            break;
        default:
            break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
        MobclickAgent.onPause(this);
    }

    
    
}
